Hello I have made, development, a webApp called PalestraGes which contains the various folders. Now I need to move it to production.
The provider told me to create a subdomain and upload everything there.
I created the subdomain and copied all the files and folders.
I also accessed the command line, and tried installing django via this command and everything seems to have gone well
Now I would like to install the virtual environment. I specify that I copied the folder venv should I still create the environment or is it already there ?
Thanks
Hello,
As stated in this stackoverflow reply : python - Virtualenv does not work after copy / paste from antoher computer. Why? - Stack Overflow , you may find some problems if you do this. The example used in this link is about a copy-paste situation, but yours is quite similar. The answer is also stated in the link, but i will explain it a bit further.
The usual way, when you develop an app on your computer (like yours) and you want to upload it to a server (like you did with your subdomain), is to create a file on your local computer that lists all of the packages that you installed in your virtual environment, to also upload that file to your server, and then to use that file to install all these packages on your server. It is best practice to not directly upload your virtual environment files to your server ; if you used git, you can search how to ignore these files when you make a commit with a .gitignore file for exemple. In this case, you can just delete these files on your server, with a command like rm -r venv
if you called your virtual environment venv.
To create the file on your local computer that lists all of the packages that you installed in your virtual environment, type this command in your django project (with your virtual environment activated):
pip freeze > requirements.txt
That will create a file that will list all of the dependancies in your django project. Then, after your upload this file to your server (your subdomain), you will need to create a virtual environment again on it, like this :
python3 -m venv /path/to/new/virtual/environment
And then, after you activate your virtual environment source venv/Scripts/activate
, you can install all the packages listed in your requirements.txt file with the command :
pip install -r requirements.txt
And you are good to go !
On a side note, if you do a professional django project at some point or if you want to keep using the same project for years while sometimes adding new packages or updating the old ones, it would be good to look at better, more resilient ways to handle your python dependancies with pip-tools, poetry or uv for example.
I thank you for the explanation, I will do what you told me and after I run the whole thing I will update you
I followed your directions and immediately got stuck.
I created the file requiremente.txt from development and copied it to the GymGes folder of the subdomain
I created the virtual environment and it returns the error that I don’t have permissions to create it.
I contacted the provider and they replied that up with hosting I can’t install it and I have to get a dedicated machine to run the language.
I don’t know if this is true, however if you don’t mind following me, I would like to do some testing without loading the virtual environment. I guess this is not correct however at least I would like to have the proma page displayed.
The first thing I did and update the settings file in production
# base URL for static and media files
STATIC_URL = '/static/'
MEDIA_URL = '/media/'
# Configuring the path to media files
MEDIA_ROOT = os.path.join(BASE_DIR, 'media') # Path to media files for both development and production
# debug = FALSE
DEBUG = TRUE
# Static file configuration
if DEBUG:
ALLOWED_HOSTS = [“localhost”, “127.0.0.1”]
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static'), # Path to project-level static files (if needed).
]
STATIC_ROOT = None # We do not use STATIC_ROOT in development
else:
ALLOWED_HOSTS = [“yourpersonalsoftware.co.uk”, “www.yourpersonalsoftware.it”, “Bfit.yourpersonalsoftware.co.uk”, “45.143.182.39”]
STATIC_ROOT = BASE_DIR / 'static' # Used only in production after collectstatic
STATICFILES_DIRS = [] # Not needed in production if you use STATIC_ROOT
I always get the same error back, the home page is homt.html given in urls.py
path('', home, name='home'),
- Well to help you on your first problem, not being able to create the virtual environment :
- Are you the owner of your server ? If your server is a version of Linux, which one is it ?
- Could you post the traceback / error message sent by the server when you try to create the venv and it doesn’t work ? (If it’s not a permission problem it could also be that python or pip isn’t installed on the server, so venv wouldn’t work).
- For your second problem:
- The test that you want to do is to be able to display/see your home.html page on your domain name like yourpersonalsoftware.co.uk or 45.143.182.39 ?
- When you launch django, does it display an error or a warning ?
- What is the error message that you get ? When you input the adress of your server or the name of your domain in your browser what happens ?
No, I don’t own the server
Yes, it is linux, how do I display the Linux version?
In the virtual environment installation with this command
sudo apt python3 -m venv
error returns
yourpersonal@vm23:~/Bfit.yourpersonalsoftware.com/PalestraGes$ python3 -m venv venv
The virtual environment was not successfully created because ensurepip is not available.
available. On Debian/Ubuntu systems, you need to install the python3-venv package
using the following command.
apt install python3.8-venv
You may need to use sudo with this command. After installing the python3-venv package.
recreate the virtual environment.
Command not working: [‘/var/www/vhosts/yourpersonalsoftware.it/Bfit.yourpersonalsoftware.it/PalestraGes/venv/bin/python3’, ‘-Im’, ‘ensurepip’, ‘–upgrade’, ‘–default-pip’].
I try to install it with this command
sudo apt install python3-venv
returns this error
yourpersonal@vm23:~/Bfit.yourpersonalsoftware.co.uk/GymGes$ sudo apt install python3-venv
yourpersonal is not present in the sudoers file. This incident will be reported.
yourpersonal@vm23:~/Bfit.yourpersonalsoftware.co.uk/PalestraGes$
ALLOWED_HOSTS = [“yourpersonalsoftware.co.uk”, “www.yourpersonalsoftware.it”, “Bfit.yourpersonalsoftware.co.uk”, “45.143.182.39”]
I read that in the host you should enter domain, subDomain and IP.
Is that not the case?
Do I have to start django from the command line?
How do I do it from the command line?
If it’s written debian/ubuntu systems then it’s pretty regular, the main problem here looks like you don’t have the right level of permission to use that command. If you can’t use sudo, then try to do without - i found another stackoverflow page that may adress your issue : python - How to avoid "Permission denied" when using pip with virtualenv - Stack Overflow it’s about virtualenv, which can also make a python virtual environment for you.
As for the allowed hosts, yes it all seems good.
But for running your django project in production i strongly suggest you read the documentation about it Comment déployer Django | Documentation de Django | Django , and more specifically the “how to deploy with wsgi” section, all that page will explain how to launch your django app.
If you then go with gunicorn as your choice, I recommend that you search the digitalocean tutorial on how to deploy django with gunicorn that can help you on the way and that will give you more detailed instructions.
I read the link but the main problem is that the python3-venv package, needed to create virtual environments in Python, is not installed on the system. On Debian or Ubuntu, venv is not included by default in Python and must be installed manually.
It is recommended to install virtualenv but if I remember correctly from version 3.6 it is discouraged
Version 3.8 of python is present on the server.
I’ll give you more pointers and if you can tell me if it is possible to install python in a linux subdomain and plesk interface or if someone has done it ?
Thanks
I searched for a bit and found here python - apt-get install python3-venv is not working - Stack Overflow and in other places that to install that venv package you need to use sudo and so you need root privilege. If the user you connect with can’t use sudo, in your case you were returned “yourpersonal is not present in the sudoers file” (yourpersonal being your user name i suppose), then you won’t be able to install it at all. From what i read, it means the person that has root privilege on the server should install that package with sudo and THEN you could use it too because it would become available for other users.
There might also be clues on this page python3 - Cannot install python venv on Ubuntu 20.04 after upgrading from Bionic - Ask Ubuntu .
Confirmed, I banged my head on it for venv and without having the privileges nothing. I contacted the provider and he replied that he cannot install it since the machine is shared and I would be the only one using python
From what is your experience is it mandatory to install venv ?
I admit I have small experience when it comes to professional projects with django or python web apps in general, so I think a more experienced developer could tell you more about this.
The main advantage or venv, virtualenv, uv etc. and all tools you can use to make a virtual environment in python is to have a good isolation between your main machine and the packages that you install for a particular project. You can easily delete a virtual environment, you can easily track exactly what packages and what dependancies there are in a virtual environment, and it won’t create conflicts due to contradicting packages between old and new versions of packages that you may have elsewhere on your machine.
So making a virtual environment is a good way to avoid a lot of difficulties and headaches in the long run.
But even if it’s not advised to do this, it’s not an obligation to create a virtual environment.
It depends on your task, and what you are allowed to do in your project. If you don’t encounter more permissions problems, you can directly install on your server all the dependancies listed in your requirements.txt file like so :
pip install -r requirements.txt
The main downside to that is it will be shared on your machine with the other users, and if one of them download other python packages there could be conflicts and it could cause problems for your app and theirs.
Another solution if you don’t encounter another permission problem would be to contenarize your app, with docker for example.
It might be a little hard if you don’t know docker yet or if you are not used to deploy web apps, but with this solution you will be able to isolate your project on your server.
thanks for the explanation and the link. Previously I had run pip install -r requirements.txt and fixed all the errors.
In the process of creating the virtual environment I stopped and having already the project I copied it to the subdomain but without success.
From the link you pointed me to, I will try to create a project from scratch and see how it goes on
As soon as it is finished I will update you