I am trying to run python sphinx on a CentOS-7 VM, using a virtual python 3.6.1 environment.
Steps:
Create python3 virtual environment
$ pyvenv myapp
$ source myapp/bin/activate
Install required packages for myproj
$ pip install -r requirements.txt
Install sphinx
$ pip install sphinx sphinx-autobuild
Try and run the quickstart:
$ sphinx-quickstart
File "/bin/sphinx-quickstart", line 7 in <module>
from sphinx.quickstart import main
ImportError: No module named sphinx.quickstart
pip freeze indicates that I am running the following versions:
Sphinx==1.6.3
sphinx-autobuild==0.7.1
pyvenv has been deprecated. See note in the Python library docs:
Note: The pyvenv script has been deprecated as of Python 3.6 in favor of using python3 -m venv to help prevent any potential confusion as to which Python interpreter a virtual environment will be based on.
Instead first make sure you have not already activated a virtual environment. Then issue the command:
python3 -m venv myapp
The rest of your steps look OK.
I solved the problem. I am new to python and pyvenv, and apparently it was finding a "universally" installed version of sphinx outside of the virtual environment, and that was preempting the version that was installed inside the virtualenv. I solved the problem by specifying the full path:
/full-path-to-myapp/myapp/bin/sphinx-quickstart
Related
I have different venvs in my machine in which I have python 3.10.
Now for a specific project, I realised that python 3.10 is not suitable as some libraries are still not compatible. Therefore when creating a new venv for a new project, I would like to downgrade python, say to 3.8, only for this specific venv.
How can I do that?
What should I type onto the terminal to do this?
PS: I use VS and its terminal to create venv
The recommended way by python.org
The recommended way of managing virtual environments since Python 3.5 is with the venv module within the Python Standard Library itself.
Source: https://docs.python.org/3/library/venv.html#creating-virtual-environments
That is not the same as virtualenv, which is a third party package, outside the Python Standard Library.
Source: https://pypi.org/project/virtualenv/
Dangerous to downgrade (and to upgrade)
Depending on if your system itself uses Python, it could be dangerous for system stability to change the system Python version. Your system might need exactly that version of Python. That is true with Ubuntu.
Install another version of Python
Safer than downgrading or upgrading is installing other versions of Python on the same system.
For example, in Ubuntu 20.04, to install Python 3.9:
# Update package lists
me#mydevice:~$ sudo apt update
# Add the deadsnakes repository
me#mydevice:~$ sudo add-apt-repository ppa:deadsnakes/ppa
# Install Python 3.9
me#mydevice:~$ sudo apt install python3.9
Install the venv package and create a venv virtual environment
# Install the venv package for Python 3.9
me#mydevice:~$ sudo apt install python3.9-venv
# Make a folder for venv virtual environments
me#mydevice:~$ mkdir ~/.venvs
# Create a new venv virtual environment with Python 3.9 in it
me#mydevice:~$ python3.9 -m venv ~/.venvs/my-venv-name
# Activate the new venv
me#mydevice:~$ source ~/.venvs/my-venv-name/bin/activate
(my-venv-name) me#mydevice:~$
Check versions within the venv virtual environment
# Check the Python version inside the venv:
(my-venv-name) me#mydevice:~$ python -V
Python 3.9.9
# Check the Pip version inside the venv:
(my-venv-name) me#mydevice:~$ pip3 --version
pip 21.2.4 from /home/me/.venvs/my-venv-name/lib/python3.9/site-packages/pip (python 3.9)
Deactivate the venv virtual environment
(my-venv-name) me#mydevice:~$ deactivate
me#mydevice:~$
Check versions outside any virtual environments
# Check Python version:
me#mydevice:~$ python -V
Python 3.8.10
# Check the Pip version:
me#mydevice:~$ pip3 --version
pip 20.0.2 from /home/me/.venvs/my-venv-name/lib/python3.8/site-packages/pip (python 3.8)
Install more Python versions
To install more Python versions, just change the version number from 3.9 to which ever version you choose, that is available from the deadsnakes repository.
I believe the best way to work with different python versions in isolation is pyenv, managing virtual environments can be done with pyenv-virtualenv.
I think this article from Real Python does a good job at explaining how to manage different python versions as well as different virtual environments.
For posterity, with the tools mentioned above you can do the following (once the proper python versions are installed)
pyenv virtualenv <python_version> <environment_name>
# Then activate it
pyenv local <environment_name>
Now that you've created a virtual environment in the folder, it should be picked up any time you enter the folder. VSCode should also pick it up, as per its documentation.
P.S: The reason I think it's a good approach it's because it allows you to manage python versions as well as environments with a single tool. Each version is installed only once, in one place, which should help because it reduces complexity.
Simple and recent
Supposed that you have a different version of Python installed in your system. To check use the following command to check:
> py --list
-3.10-64 *
-3.7-64
And you want to create a new virtual environment for python 3.7 on a 'test_env' directory. Run the following command:
> py -3.7 -m venv test_env
Then activate the test_env by running the following command on Windows PowerShell:
> .\test_env\Scripts\Activate.ps1
Or Linux:
$ source test_env/bin/activate
Check:
python --version
Python 3.7.0
You can have multiple python versions installed at the same time and you can create virtual environments with the needed version. Make sure you have installed the python version you need and then specify its location when you create the virtual environment:
virtualenv -p <path-to-new-python-installation> <new-venv-name>
Example:
virtualenv -p C:\Users\ssharma\AppData\Local\Programs\Python\Python38\python.exe venv38
This will create a virtual environment called venv38 with Python 3.8.
you can do it by using "virtualenv" library. It can be installed with command pip install virtualenv
then followed by command
virtualenv "name_of_your_environment" #no quotes
and then use the following code to activate your venv
"name_of_your_environment"\Scripts\activate #NOTE YOU MUST BE at your directory where you created your env.
its for VS CODE but I prefer installing conda and then creating env on conda prompt using conda which later you can access to vs code to and its easy to activate that env from anywhere just type conda activate 'name_of_your_env' on vs terminal
You can use anaconda:
conda create -n mypython3.8 python=3.8
For merely the sake of enough porridge in your stew.
I set up a venv for my project and ran pip install --upgrade google-api-python-client google-auth-httplib2 google-auth-oauthlib to get the Google packages (after activating the venv).
When I attempt to run the Python Quickstart code (which I copy/pasted into my VScode project) I get the error: ModuleNotFoundError: No module named 'google_auth_oauthlib'. I ran pip list and verified that version 0.4.2 is installed.
My setup:
MacOS 10.15.07
VScode 1.53.2
venv Python 3.7.7
To be sure, activate the virtual environment in the shell you will use to run the quickstart and run pip freeze. Additionally make sure your the python you are using is the one in the venv. You can do which python
Maybe try using pip3 install instead? This is what fixed it for me.
I'm in the middle of switching from VS Code to Atom and I'm trying to set up a virtual environment for my python project.
It was pretty easy to do in VS Code, I'd run the following script and it would automagically start using the new env (with all the required packages) when I'd run the script:
python3 -m venv my_env
source my_env/bin/activate
pip3 install -r requirements.txt
Now I'm trying to set up Hydrogen to work the same way. When I run lines of code inline with Hydrogen, I want them to be run in a virtual environment that has the imported modules I need from a requirements.txt file.
I was able to install the python3 kernel with the following commands:
python3 -m venv my_environment_name # create a virtual environment
source my_environment_name/bin/activate # activate the virtual environment
python -m pip install ipykernel # install the python kernel (ipykernel) into the virtual environment
python -m ipykernel install
And Atom is able to see it:
Screenshot
However, I'm still puzzled as how to install my dependencies into the kernel. And if I do install my dependencies there, I don't want my next python projects to have all those modules in there. I'd love to have the fresh-slate that virtual environments promise.
Any help here would be appreciated. Has anyone had experience setting up a virtual environment that can be used by the Hydrogen package?
Ok, after some more experimentation, I was able to connect to a kernel that I had installed my requirements.txt into.
Here are the steps I took:
python3 -m venv env
source env/bin/activate
# make sure requirements.txt has ipykernel in it
pip3 install -r requirements.txt
python3 -m ipykernel install --user --name=env
Then in Atom, press cmd-shift-p and find Hydrogen: Update Kernels.
Or manually Packages->Hydrogen->Update Kernels
After, I was able to use the kernel by doing cmd-shift-p again and selecting Hydrogen: Start Local Kernel and selecting env.
When I would run import statements via Hydrogen (selecting them and pressing cmd-enter), they would now know what to import! Horray!
Windows Version.
This is the windows version of the excellent answer by Tim Estes. Without the use of requirements.txt in case some folks such as myself would like to install ipykernel separately. (Thanks so much Tim, it saved me a ton of time)
python -m venv env
env/bin/activate
pip install ipykernel
python -m ipykernel install --user --name=env
Similar to Tim's answer, go ahead and update kernels at Atom.
Packages->Hydrogen->Update Kernels
I'm having a little trouble with virtualenv on Mac OS X Yosemite. After I couldn't run virtualenv at all first, I installed Python 3 via brew (previously I installed it via the package on python.org). I linked this installation of python3, updated pip and ran pip3 install virtualenv. When I try to run virtualenv (e.g. $ virtualenv --python=python3 ../virtualenv), I get the following error message.
Traceback (most recent call last):
File "/usr/local/bin/virtualenv", line 7, in <module>
from virtualenv import main
File "/usr/local/bin/virtualenv.py", line 7, in <module>
from virtualenv import main
ImportError: cannot import name 'main'
Can anybody help me with this?
After my upgrade to Fedora 32 I had the same issue which lead me to this question:
ImportError: cannot import name 'main' from 'virtualenv'
In my case I actually seemed to have both /usr/local/bin/virtualenv as well as $HOME/.local/lib/python3.8/site-packages/virtualenv/__init__.py.
Removing the user virtualenv version and reinstalling it into the system with root fixed the issue:
pip uninstall virtualenv
sudo pip install virtualenv
Your virtualenv executable /usr/local/bin/virtualenv is importing the virtualenv package /usr/local/bin/virtualenv.py. My guess is that package is not the one the executable should really be importing. The reason it is choosing that one is because it is in the same directory.
First, check where the real virtualenv package is. In the python3 terminal:
>>> import virtualenv
>>> virtualenv.__file__
If it is not /usr/local/bin/virtualenv.py, then the simplest way to get /usr/local/bin/virtualenv to import it instead of /usr/local/bin/virtualenv.py is to delete /usr/local/bin/virtualenv.py (or so you can easily undo this if it doesn't work, simply rename virtualenv.py to something else like xvirtualenvx.py).
I received this error after upgrading Ubuntu 18.04 LTS to 20.04 LTS. So there were two problems all at once. First the python version was still running 2.x and doing a simple update or try to uninstall (apt-get remove virtualenv) of virtualenv did not help at all. But I found a solution. First let 20.04 LTS 'know' the times of using old python is over:
sudo apt-get install python-is-python3
Then test it and open a console to get the version string with python -V; by now it should be showing something like Python 3.8.5. Fine.
Next step is to solve the virtualenv problem. I tried to find out, which executable was run with which virtualenv and it showed: $HOME/.local/bin/virtualenv. Hmmkay, somehow the system wasn't using the /usr/bin/virtualenv executable. I thought maybe I let the directory become invisible (a.k.a. renaming) and maybe the system will go on a hunt for an alternative virtualenv running:
mv $HOME/.local/bin/virtualenv /home/USER/.local/bin/virtualenv_OLD
Then I simply changed into a playground-directory and ran virtualenv donaldknuth and behold - it worked. To be sure I ran another which virtualenv and the system returned a /usr/bin/virtualenv. Last check to do was activating the new virtual environment:
source $HOME/playground/donaldknuth/bin/activate
The terminal changed and it worked fine. Solution
EDIT:
Based on Pierre B.'s suggestion you may have to restart your Shell. The command hash -d virtualenv will delete the stored location of virtualenv from the shell's cache and determine the correct path right now. (Sources: https://www.computerhope.com/unix/bash/hash.htm, https://unix.stackexchange.com/questions/5609/how-do-i-clear-bashs-cache-of-paths-to-executables)
Similarly to some others here, I had multiple installations of virtualenv. Not sure where the extra one came from, but I had these two:
/usr/local/bin/virtualenv
/usr/bin/virtualenv
One is from apt install of virtualenv, the other from pip install of virtualenv.
This happened when upgrading to Ubuntu 20.04.
On Linux Mint 20, I had to switch default Python interpreter to python3
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3 1
Then remove existing virtualenv and reinstall via pip and python3:
rm ~/.local/bin/virtualenv
apt remove python3-virtualenv
sudo pip install virtualenv
I'm using Fedora 21. Installed the python3-tkinter package using yum install python3-tkinter. The package gets stored in the /usr/lib64/python3.4 directory. Is there a way to use pip to install tkinter?
Have a virtualenv setup with python3. When I try to run my program within that virtualenv I get:
ImportError: No module named 'tkinter'.
Does it make sense to copy the package directories from /usr/lib64/python3.4 to the site_packages folder associated with the virtualenv?
I was using python 3.3.2 interpreter. Turns out the default packages installed when running the command yum install python3-tkinter are set to work with python 3.4.1 interpreter. Configuring my virtualenv to use the python 3.4.1 interpreter proved to be the solution as the python interpreter was then able to find the required libraries in it's path.