I have a VPS running Centos6, which has Python2.6 pre-installed. I have installed Python3.6 on this path
/usr/local/bin/python3.6
I have installed virtualenv using pip which is at
/usr/local/bin/virtualenv
I then created a virtualenvwrapper.sh file under /etc/profile.d (I have been advised this is what I need to do) which is generating an error on start up:
/usr/bin/python: No module named virtualenvwrapper
virtualenvwrapper.sh: There was a problem running the initialization hooks.
If Python could not import the module virtualenvwrapper.hook_loader,
check that virtualenvwrapper has been installed for
VIRTUALENVWRAPPER_PYTHON=/usr/bin/python and that PATH is
set properly.
virtualenvwrapper is located at /usr/local/bin/python3.6/site-packages
As I am novice using Linux I didn't think the code in the file virtualenvwrapper.sh (under /etc/profile.d)
export WORKON_HOME=$HOME/.virtualenvs
export PROJECT_HOME=$HOME/Devel
source /usr/local/bin/virtualenvwrapper.sh
UPDATE
I have now edited the virtualenvwrapper.sh file under /etc/profile.d as follows
export WORKON_HOME=$HOME/.virtualenvs
export PROJECT_HOME=$HOME/Devel
export VIRTUALENVWRAPPER_PYTHON=/usr/local/bin/python3.6
export VIRTUALENVWRAPPER_VIRTUALENV=/usr/local/bin/virtualenv
source /usr/local/bin/virtualenvwrapper.sh
This has worked fine - I can now run the virtualenvwrapper commands. Is it pk that all of the start up files have been placed in the .virtualenvs folder (eg initalize, postactivate etc) - which means that when I create a virtualenvironment, that will be placed alongside these files? Is there a better way to do this?
Many thanks
Answering your second question: this is how virtualenvwrapper works. It puts global initialization files into ~/.virtualenvs; these files will be run (sourced, actually) for every virtual env. Also there will be separate initialization files for every virtual env.
Having all virtual envs in ~/.virtualenvs helps virtualenvwrapper to manage all envs.
Related
I need to provide my version of Python and packages for a project.
How can I do that?
I tried:
sudo apt-get install python3-venv
virtualenv my-env -p python3
source tutorial-env/bin/activate
This should show installed packages, but it shows:
pip list
DEPRECATION: The default format will switch to columns in the future. You can use --format=(legacy|columns) (or define a format=(legacy|columns) in your pip.conf under the [list] section) to disable this warning.
pip (9.0.1)
pkg-resources (0.0.0)
setuptools (39.0.1)
This is also suspicious:
(tutorial-env) linux#LINUXMINT:~$ pip freeze > requirements.txt
(tutorial-env) linux#LINUXMINT:~$ cat requirements.txt
pkg-resources==0.0.0
(tutorial-env) linux#LINUXMINT:~$ python -m pip install -r requirements.txt
Requirement already satisfied: pkg-resources==0.0.0 in ./tutorial-env/lib/python3.6/site-packages (from -r requirements.txt (line 1))
And I cannot find requirements.txt in my directory.
Pass the version of python when creating your virtualenv like this:
virtualenv my-env -p python3
I think you should not use any external module for creating a virtual environment.
You can create a Virtual Environment by using venv attribute of Python in Command Line.
What is the syntax?
The syntax is pretty simple.
C:\>python -m venv path\to\where\you\want\to\create\it
It can be easily done, and you can get a reference from the example below:
C:\>python -m venv "C:\Users\Bhavyadeep\Desktop\Discord Bots\Bot" 1\Bot-1-env
Here the name Bot-1-env is the name of the folder which will be created on execution of the command, and it doesn't have to exist.
What if I am using an IDE (like VS Code), then how will I create a virtual environment?
Creating a Virtual Environment in an IDE is much easier than creating it using CMD. In CMD you need to specify the full path of the directory where the Environment has to be created whereas in an IDE you can create one using its own terminal and also there won't be any need of adding and full path to the directory.
Syntax for IDEs with their Terminals is:
C:\>python -m venv My-Env
This would simply create a Virtual Environment in the folder of the project you are working on in the IDE. If you still want to create it using full path you can do the same as above in the Terminal of IDE.
Example with Images and Code in One Step:
My target directory would be the Desktop for now to explain.
Write and Execute the command in Command Line.
I entered the line in the image and pressed Enter.
Here the name of the Folder would be Example-Venv and it doesn't exist. This command created a folder with that name and created that a Virtual Environment.
This command created a folder, and it can be seen in the picture below.
Now you can use it anywhere you want by simply making this folder as the Interpreter.
How to set interpreter?
The following links would explain:
Pycharm: https://www.jetbrains.com/help/pycharm/configuring-python-interpreter.html
VS Code: https://code.visualstudio.com/docs/python/environments#:~:text=To%20select%20a%20specific%20environment,or%20library%20versions%20as%20needed.
Spyder: https://www.spyder-ide.org/blog/release-spyder-330/#:~:text=Just%20set%20the%20path%20under,start%20in%20the%20selected%20environment.
Sublime: https://medium.com/#hariyanto.tan95/set-up-sublime-text-3-to-use-python-3-c845b742c720
I was glad to help! If you still get any problem, please feel free to ask in the comments and I would gladly help you! :)
Thank You! :)
I have a script (personal, not for distribution) that works on one of my computers. I want edit it on another computer.
On the first computer, the script was created without a virtual environment. I want to start using them on the second computer.
I have these set up on the second computer.
Windows 10
VS Code 1.45.1
Python 3.8.3
Paths:
Python - C:\Python38
Virtual environments - C:\Users\<User>\Envs
Projects folders - C:\Users\<User>\Documents\python-projects
Environmental variable WORKON_HOME is set to virtual environments path
I copied the script to the project folder. After activating the venv with workon, I pip installed the external modules on the second computer.
I can see the modules when the virtual environment is activated and not when it is deactivated. I believe this means the virtual environment is working and the modules were properly installed.
However, I keep getting the ModuleNotFound error for the external modules. I've tried uninstalling and reinstalling the venv and the modules but I keep getting the error. To ensure it's not the computer, I deactivated the venv and installed the modules and the script worked. I even tried creating a new script with only import requests and I still get ModuleNotFound.
What do I need to do to get the script to use the modules in the virtual environment?
You can do this multple ways. Some are listed here.
1.using IDLE
>>> help("modules")
2.using PIP
$ pip list
3.Using Anaconda
$ conda list
Before doing this, activate particular environment.
I installed JupyterLab with
pip3 install jupyterlab --user
Yet, when trying I try to launch it (jupyter lab), I get the following error:
Error executing Jupyter command 'lab': [Errno 2] No such file or directory
The JupyterLab installation guide on github says that: "If installing using pip install --user, you must add the user-level bin directory to your PATH environment variable in order to launch jupyter lab"
But I don't what that means, I greatly appreciate any help. I am using Ubuntu 18.04
As said by the guide itself you need to add the user-level bin directory to your PATH environment variable, in order to do so you need at first spot which is the bin folder where Jupyter lab has been installed, and after that you can add that path with a simple command:
export PATH=$PATH:/path/to/your/jupyterlab/bin/directory
and it's done. You can check if you added it by running this other command:
echo $PATH
And you should see the content of PATH variable.
This method though will just add that variable for the current shell, meaning that when you close the terminal you lose the change in the variable. In order to make it permanent you need to edit another file which is ~/.bashrc.
One thing though, it's really important that you just add this line to the file:
PATH=$PATH:/path/to/your/jupyterlab/bin/directory
without changing all the rest of the file if you don't know what you are doing.
To give you a recap on what to do to make it permanent open a new shell and type:
gedit ~/.bashrc
This will open the file where you need to add the "export PATH...etc" command right at the end of the file in a new line. Then save the changes and reboot, from now on you should be able to open Jupyter lab directly from a shell with the command:
Jupyter lab
-bash: /usr/local/bin/python3: No such file or directory
virtualenvwrapper.sh: There was a problem running the initialization hooks.
If Python could not import the module virtualenvwrapper.hook_loader,
check that virtualenvwrapper has been installed for
VIRTUALENVWRAPPER_PYTHON=/usr/local/bin/python3 and that PATH is
set properly.
The error comes from a broken installation of virtualenvwrapper. It seems you've installed virtualenvwrapper using /usr/local/bin/python3 but later removed that python.
The error comes from ~/.bashrc. Edit it, search for something like source virtualenvwrapper_lazy.sh or . virtualenvwrapper.sh and comment out or remove the line.
1. Check virtualenvwrapper is installed.
2. Open your .bashrc file.
3. Run the below commands:
export WORKON_HOME=$HOME/.virtualenv
source /usr/local/bin/virtualenvwrapper.sh
workon <virtual_env_name_of_your_choice>
which python
4. Check the python version.
5. If you want to change the python of your virtualenv, then follow the steps below:
vi .bashrc or vi .zshrc
alias python=python3
6. Open a new terminal.
which python
workon
which python
You are all set with virtualenv set to python3 by default.
I get the following message when trying to run virtualenv . --no-site-packages
You are attempting to install a package to a directory that is not
on PYTHONPATH and which Python does not read ".pth" files from. The
installation directory you specified (via --install-dir, --prefix, or
the distutils default setting) was:
/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages/
and your PYTHONPATH environment variable currently contains:
''
However, when I echo my PYTHONPATH, I see the path from the default setting:
/usr/local/python-3.3.1/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages/:/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages/
So my PYTHONPATH does contain the directory I am installing to.
Anyone have any ideas what is going on?
Turns out I needed to use virtualenv-3.3, not virtualenv.