I am working with multiple versions of Python, so I don't want to put any one of them in my windows path. I discovered that I can create a virtualenv (under Settings -> Project: X -> Project Interpreter and that will allow me to specify which python exe to use, but also where to get other useful programs, like pip. The end result is that using Virtualenv I can type "pip" or "python" in the terminal in Pycharm and use it as a regular terminal.
But now I'm working with Anaconda so I don't want to use virtualenv and I want to use conda instead. This works fine, but it doesn't seem to put anything into the path. (This is particularly important because where I intend to keep my project is not near my path to the python environment conda created.)
So if I type "pip" or "python" in the pycharm terminal, it doesn't recognize them. I have to specify a huge path to get it to work right.
I've been googling around and trying to find a solution to this problem. I've found a number of supposed solutions, but so far none have worked.
So, for example, I've already tried Add Content Root under project structure. This does nothing. I've tried add local, which seems to just screw everything up. (Presumably this is a way to create a local environment that I shouldn't be touching in conda.) I've tried adding a PathConfig.pth file. So far nothing works.
Remember, I don't want to add anything into the windows path (which obviously would solve my problem) because then it won't know which specific environment I want at the moment.
Related
I create a virtual environment; let's say test_venv, and I activate it. All successful.
HOWEVER, the path of the Python Interpreter doesn't not change. I have illustrated the situation below.
For clarification, the python path SHOULD BE ~/Desktop/test_venv/bin/python.
>>> python3 -m venv Desktop/test_venv
>>> source Desktop/test_venv/bin/activate
(test_venv) >>> which python
/usr/bin/python
Please make sure to read Note #2.
This is what you should do if you don't want to create a new virtual environment:
In venv/bin folder there are 3 files that store your venv path explicitly
and if the path is wrong they take the normal python path so you should change the path there to your new path.
change: set -gx VIRTUAL_ENV "what/ever/path/you/need" in activate.fish
change: VIRTUAL_ENV="what/ever/path/you/need" in activate
change: setenv VIRTUAL_ENV "what/ever/path/you/need" in activate.csh
Note #1:
the path is to /venv and not to /venv/bin
Note #2:
If you reached this page it means that you are probably not following Python's best practice for a project structure.
If you were, the process of creating a new virtual environment was just a matter of one command line.
Please consider using one of the following methods:
add a requirements.txt to your project - not recommended.
implement an setup.py script - for real projects.
use a tool like Poetry - just like the latter though somewhat user-friendlier.
Thanks to Khalaimov Dmitrii, I didn't thought it was because I moved the folder.
It is not an answer specifically to your question, but it corresponds the title of the question. I faced similar problem and couldn't find solution on Internet. Maybe someone use my experience.
I created virtual environment for my python project. Some time later my python interpreter also stopped changing after virtual environment activation. Similar to how you described.
My problem was that I moved the project folder to a different directory some time ago. And if I return the folder to its original directory, then everything starts working again.
There is following problem resolution. You save all package requirements (for example, using 'pip freeze' or 'poetry') and remove 'venv'-folder (or in your case 'test_venv'-folder). After that we create virtual environment again, activate it and install all requirements.
This approach resolved my problem.
Check the value of VIRTUAL_ENV in /venv/bin/activate . If you renamed your project directory or moved it, then the value may still be the old value. PyCharm doesn't update your venv files if you used PyCharm to rename the project. You can delete the venv and recreate a new one if the path is wrong, or try the answer that talks about where to change it.
I am working on a commit-msg git hook. Everything works except importing of specific modules. The bad behaviour is specific to the use of the vs-code GUI git bar (little check mark at the top of the source control panel) with pip installed modules. If I use git commit -m "some message" through the terminal, everything works perfectly.
But, when I use the vs-code GUI, I get an import error.
Thinking it may be an environment issue, I ensured that the module needed was available globally by pip installing the needed module to my machine outside of any virtual environment. It didn't help.
To see what's going on, I wiped my hook and replaced it with nothing more than help("modules") to see if my modules were indeed there. They were not. So I ran
import os
dir_path = os.path.dirname(os.path.abspath(__file__))
print(dir_path)
This printed my path to the file being ran and was the path expected.
I opened a new terminal outside of vs-code with no virtual environment enabled and ran python3 path/to/file to see if my module would show up there. It did.
Additionally, it is there if I run it in the vs-code terminal as well. The only place it seems to be missing is wherever the vs-code git GUI pulls it's package list from.
Where does vs-code git get it python package list and how can I ensure that either; 1) it points to my normal python package list or 2) I can add the needed modules into it's path?
Thanks
EDIT: Thanks to the intelligent suggestion of John Szakmeister left in the comments, I have configured my hook to call env. I then initiated the hook through the terminal and through the GUI to compare.
Using the GUI adds a few variables, mostly for logging purposes and error handling. But there are a couple that I don't quite know what they do. APPLICATION_INSIGHTS_NO_DIAGNOSTIC_CHANNEL=true and
ELECTRON_RUN_AS_NODE=1
However, being that it works in terminal but not GUI, I have to assume that the added variables are not the culprit. So, instead I look at what is different from one to the other.
Outside of the additional VS-Code specific variables, there really isn't very much that is coming up different.
Shell level is 2 through terminal but only 1 through the GUI (interesting...)
Terminal has extra variables TERM=xterm-256color and TERM_PROGRAM_VERSION=1.46.0 that aren't present in GUI env.
Additionally, there are some colors that are set in terminal but not the GUI.
Lastly, there is one variable that is in both that is different. GUI gives _=/usr/share/code/code while terminal yields _=/usr/bin/git. I assume that these refer to what program initiated the terminal but I haven't been able to verify that.
That's about it. PATH is the same for both, as is SHELL, as are just about all of the GIT_anything variables (outside of the fact that the GUI sets a pager that the terminal does not)
I am using the latest version of Pycharm(pycharm-2017.1) and have used Pycharm for a couple of years without issue. However, I have never written Python 3 scripts with it before, and have issues now. I am getting red underlined syntax errors which appear to clearly indicate that Pycharm doesn't understand that I'm using Python 3.
In 'Edit configurations', I have set Python 3.x to be the default interpreter... I'm not sure what I'm doing wrong.
Note: I have the correct python3 shebang lines in my scripts, and running them both straight from a terminal, or even by pressing 'run' in Pycharm works no problem.. this latter fact just adds to the mystery.
I also tried adding (with the '+' in 'Edit configurations', specific profiles for each of the scripts concerned, with Python3.x as the interpreters, to no avail)
Thanks for reading
The Edit configurations settings refers to the running options only.
To use python3 at project level you need to change the interpreter in File > Settings > Project: project-name > Project Interpreter
This allow to have correct syntax-checking, import-checking and others
I've got anaconda installed and was able to create a Python 3.3 environment. I can switch to it and conda info -e shows that I've switched.
However, I'm confused about what to set my PATH variable to. If I hard code it to the exact env then it works, but I thought that the purpose of conda was to be able to switch easily, as well as update and maintain various environments separately.
Perhaps I misunderstood and there's no way around setting my PATH everytime...
In the Windows cmd shell, use the activate and deactivate commands to change the PATH automatically. For instance, if your environment is called python3, run python3 to "activate" (i.e., add to the PATH) the python3 environment. Use deactivate to remove it.
Background: I am trying to run a windows-installed version of python from within cygwin, rather than the cygwin version (some packages I am using prefer the non-cygwin version). The following post got me up and running: Using Windows Python from Cygwin. However, I am now having an issue.
Problem: The PYTHONPATH variable does not seem to be working for me. I try "import module1" when I am using python in an arbitrary directory. module1 is located here: c:\cygwin\home\mcsefl\testFolder\module1.py. And my PYTHONPATH variable is set in my .bashrc file in the following way: "export PYTHONPATH=$PYTHONPATH:/home/mcsefl/testFolder". However I still get a "No module named..." error.
Alternate tries: Since I am running the Windows Python, I thought perhaps python did not care about the cygwin environment variables (not sure if this is true..?) So, I also tried to set the windows PYTHONPATH env variable in the control panel of windows with no luck as well.
Thanks in advance
I deleted the line in my .bashrc file that tried to set the PYTHONPATH variable and just let it be set by Windows. This corrects the problem.
Side note: The title for this Question is a misnomer now that I re-read it. My issue was resolved without addressing that overall question